package com.peas.platform.module.icims.base.mdm.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.base.mdm.adapter.repository.AirportRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.po.AirportPo;
import com.peas.platform.module.icims.base.mdm.domain.AirportDomain;
import com.peas.platform.module.icims.base.mdm.dto.AirportDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class AirportDomainImpl extends BaseDomainImpl<AirportDto, AirportPo> implements AirportDomain {

    @Resource
    private AirportRepository airportRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<AirportPo> getMapper() {
        return airportRepository;
    }

    @Override
    public Class<? extends BaseRepository<AirportPo>> getRepositoryClass() {
        return AirportRepository.class;
    }

    @Transactional
    @Override
    public int save(AirportDto dto) {
        if (StrUtil.isBlank(dto.getAirportCode())) {
            String code = sequenceService.genSequence("base", "AirportDto", "AC", 4, 1);
            dto.setAirportCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("airportCode", dto.getAirportCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(AirportDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("airportCode", dto.getAirportCode());
        return result;
    }

}
